package com.liull.netty.nio.future;

import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 20:45 2021/7/24
 * @Modified By:
 */
@Slf4j
public class TestJdkFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1.创建一个线程池
        final ExecutorService threadPool = Executors.newFixedThreadPool(4);
        final Future<Integer> future = threadPool.submit(() -> {
            Thread.sleep(1000);
            return new Random().nextInt(10);
        });
        // 阻塞等待callable任务完成
        log.debug("阻塞等待callable任务完成之后再返回结果");
        final Integer result = future.get();
        log.debug("" + result);
        threadPool.shutdown();
    }
}
